//
//  STRetweetView.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/18.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

/// 转发文本字体大小
private let textFont:CGFloat = 14.0

class STRetweetView: UIView {

    
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        // 设置
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(textFont)
        label.numberOfLines = 0
        // 最大宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * cellMargin
        
        label.text = "被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容"
        return label
    }()
    
    private lazy var pictureView:STPictureView = STPictureView()
    
    /// 被转发的模型
    var retweetStatus:STData?{
        didSet{
//            STPrint(items: "转发数据传过来了==>\(retweetStatus)")
            if retweetStatus == nil {
                textLabel.text = nil
            } else {
                // 设置被转发微博内容
                // @被转发微博用户的名称: 被转发微博的内容
                let name = retweetStatus?.user?.screen_name ?? "未知用户名"
                let text = retweetStatus?.text ?? ""
                textLabel.text = "@\(name): \(text)"
            }
            /// 设置数据源
            pictureView.pictureArray = retweetStatus?.pictureArray
            /// 更新约束
            updateUI()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor.yellowColor()
        setupUI()
    }
    
    private func setupUI(){
        addSubview(textLabel)
        addSubview(pictureView)
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self).offset(cellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
//            pictureView.backgroundColor=UIColor.cyanColor()
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(cellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        /// 底部约束
        /// 被转发底部参照配图底部偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(cellMargin)
        }
    }
    /// 更新UI约束
    private func updateUI() {
        textLabel.snp_updateConstraints { (make) -> Void in
            let margin = retweetStatus == nil ? 0 : cellMargin
            make.top.equalTo(self).offset(margin)
        }
        /// 重新更新配图的顶部
        pictureView.snp_updateConstraints { (make) -> Void in
            var margin: CGFloat = cellMargin
            if retweetStatus == nil {
                // 没有被转发微博,不要顶部间距
                margin = 0
            } else if retweetStatus?.pictureArray == nil || retweetStatus?.pictureArray.count == 0 {
                // 配图没有图片要显示
                margin = 0
            }
            make.top.equalTo(textLabel.snp_bottom).offset(margin)
        }
        
        /// 被转发view的底部
        self.snp_updateConstraints { (make) -> Void in
            let margin = retweetStatus == nil ? 0 : cellMargin
            make.bottom.equalTo(pictureView).offset(margin)
        }
    }
}
